use cross_rs::sync::statis::Statis;
use utils_rs::ipc::{client::IpcClient, types::{IpcExt, LpcExt}};
use once_cell::sync::Lazy;

/*
windows test data:
client send(is_res: false) sum: 127197
client recver(is_res: true) sum: 7945

linux test data:
client send(is_res: false) sum: 402645
client recver(is_res: true) sum: 11032
如上所示, linux debug 模式下, 单向并发达到了40万

*/
fn main() {
    utils_rs::log4rs::log4rs_mod::init().unwrap();
    let client = IpcClient::new("./Server_key", 1).unwrap();
    client.recver(|_data, call| {
        if let Some(call) = call {
            call.response(b"1231gfdgdsgd45342312".to_vec()).unwrap();
        }

        // 测试并发性能的代码, 生产环境不要使用
        // test bench code; production is not need
        static RECKON: once_cell::sync::Lazy<Statis> =
            once_cell::sync::Lazy::new(|| {
                Statis::new(|v| println!("client recver sum: {v}"))
            });
        RECKON.add();
    });

    loop {
        if let Err(e) = client.send_res("1231fdertrrdgdfg2312") {
            eprintln!("client.send: {e:?}");
            std::thread::sleep(std::time::Duration::from_millis(1 * 1000));
            continue;
        }

        // 测试并发性能的代码, 生产环境不要使用
        // test bench code; production is not need
        static RECKON: once_cell::sync::Lazy<Statis> =
            once_cell::sync::Lazy::new(|| {
                Statis::new(|v| println!("client send(is_res: true) sum: {v}"))
            });
        RECKON.add();
    }
}


pub static LPC_CLIENT: Lazy<IpcClient> = Lazy::new(|| {
    IpcClient::new("./Server_key", 5).expect("Failed to start lpc server")
});